﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Xpo;
using BusinessObjects;
using System.Linq;

namespace UI
{
    public partial class frmTaiKhoan_Edit : FormBase
    {
        public frmTaiKhoan_Edit()
        {
            InitializeComponent();
        }

        public TaiKhoan ItemEdit { get; set; }

        private void frmTaiKhoan_Edit_Load(object sender, EventArgs e)
        {
            try
            {
                txtTaiKhoan.DataBindings.Add(new Binding("Text", ItemEdit, "TenTaiKhoan"));
                txtHo.DataBindings.Add(new Binding("Text", ItemEdit, "Ho"));
                txtTen.DataBindings.Add(new Binding("Text", ItemEdit, "Ten"));
                if (ItemEdit.TenTaiKhoan != string.Empty && ItemEdit.TenTaiKhoan != null)
                    txtTaiKhoan.Enabled = false;
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (CheckInput())
                {
                    if (txtMatKhau.Text != string.Empty)
                        ItemEdit.MatKhau = txtMatKhau.Text;
                    this.DialogResult = DialogResult.OK;
                }
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private bool CheckInput()
        {
            bool result = true;
            dxErrorProvider1.ClearErrors();
            if (txtTaiKhoan.Text == string.Empty)
            {
                dxErrorProvider1.SetError(txtTaiKhoan, "Chưa nhập");
                result = false;
            }
            if (ItemEdit.TenTaiKhoan == string.Empty || ItemEdit.TenTaiKhoan == null)
            {
                if (txtMatKhau.Text == string.Empty)
                {
                    dxErrorProvider1.SetError(txtMatKhau, "Chưa nhập");
                    result = false;
                }                
            }
            if (txtMatKhau.Text != string.Empty && txtMatKhau.Text == txtNhapLaiMatKhau.Text)
            {
                dxErrorProvider1.SetError(txtNhapLaiMatKhau, "Nhập khác mật khẩu");
                result = false;
            }
            return result;
        }
    }
}